library(ggplot2)
wine <- read.csv("https://greymonroe.github.io/PLS_206/data/winedata.csv")
cassava <- read.csv("https://greymonroe.github.io/PLS_206/data/cassava.csv")
chem <- wine[, -1]Problem Set 7 Key: Principal Component Analysis
PLS 206 — Applied Multivariate Modeling
Try the problems yourself before consulting this key. Every number below was checked in R 4.5 against data/winedata.csv (178 wines, 13 chemicals) and data/cassava.csv (1006 landraces) on 2026-11-08. PC signs are arbitrary: if yours are flipped, the answers are the same with the directions reversed.
Part 1: Setting Up the PCA
Q1. The variances range from 0.015 (NonFlavPhenols) to 99,167 (Proline); Mg is 204, everything else is below 12. Unscaled, PC1 explains 99.8% of the variance and its loading on Proline is −1.000 (all others ≤ 0.02 in size). PC1 is just Proline, because Proline is measured in units that make its numbers hundreds of times bigger. The chemicals are on different scales and different units, so each must be scaled to variance 1 before PCA; otherwise the axes report units, not chemistry.
sapply(chem, var)
pca_raw <- prcomp(chem)
summary(pca_raw)$importance[2, 1:2]
round(pca_raw$rotation[, 1], 3)Q2. PC1 36.2%, PC2 19.2%, PC3 11.1%; the first six together 85.1%.
pca <- prcomp(chem, scale. = TRUE)
summary(pca)$importanceQ3. Eigenvalues: 4.71, 2.50, 1.45, 0.92, 0.85, 0.64, … Kaiser keeps 3; the 80% rule keeps 5 (80.2%); the scree elbow is at 3 or 4 (the curve flattens after PC3; any defensible reading is fine). The rules ask different questions: Kaiser keeps PCs that carry more than one original variable’s worth of variance, the 80% rule sets a target for total information kept, and the elbow looks for where extra PCs stop paying. Checking several shows how sensitive the choice is.
plot(pca, type = "l")
ev <- pca$sdev^2
sum(ev >= 1)
which(cumsum(ev) / sum(ev) >= 0.8)[1]Part 2: What the Axes Mean
Q4. Correlations (sign as R 4.5 returns them):
| PC1 | PC2 | |
|---|---|---|
| Flav | −0.92 | 0.01 |
| Phenols | −0.86 | −0.10 |
| OD | −0.82 | 0.26 |
| Proa | −0.68 | −0.06 |
| NonFlavPhenols | 0.65 | −0.05 |
| Hue | −0.64 | 0.44 |
| Proline | −0.62 | −0.58 |
| MalicAcid | 0.53 | −0.36 |
| AlcAsh | 0.52 | 0.02 |
| Color | 0.19 | −0.84 |
| Alcohol | −0.31 | −0.76 |
| Ash | 0.00 | −0.50 |
| Mg | −0.31 | −0.47 |
|r| ≥ 0.5 with PC1: Flav, Phenols, OD, Proa, NonFlavPhenols, Hue, Proline, MalicAcid, AlcAsh. With PC2: Color, Alcohol, Proline, Ash. One end of PC1 is wines rich in flavonoids and phenols (high Flav, Phenols, OD, Proa) and low in non-flavonoid phenols and malic acid; the other end is the reverse. One end of PC2 is deeply coloured, high-alcohol, high-proline wines; the other end is pale, lower-alcohol wines.
round(cor(chem, pca$x[, 1:2]), 2)
round(t(t(pca$rotation) * pca$sdev)[, 1:2], 2) # same numbersPart 3: The Wines in PC Space
Q5. The three cultivars form three clouds with little overlap. PC1 separates barolo (mean score −2.28) from barbera (+2.74): barolo is the flavonoid- and phenol-rich wine, barbera the low-flavonoid, high-malic-acid one. Gringnolino sits in the middle of PC1 (0.04) and is separated from both by PC2 (mean +1.64 vs about −1.0 to −1.2): it is the paler, lower-alcohol, lower-proline wine.
scores <- data.frame(pca$x, Cultivar = wine$Cultivar)
pct <- round(100 * pca$sdev^2 / sum(pca$sdev^2))
ggplot(scores, aes(PC1, PC2, colour = Cultivar)) +
geom_point() +
coord_fixed() +
labs(x = paste0("PC1 (", pct[1], "%)"), y = paste0("PC2 (", pct[2], "%)"),
colour = "Cultivar", title = "Wines in PC space") +
theme_classic()Q6. PC1 = −2.19, PC2 = −0.82. That is almost on the barolo centroid (−2.28, −0.97; distance 0.17), and far from barbera (4.95) and gringnolino (3.32). Most likely a barolo.
new_wine <- data.frame(Alcohol = 13.75, AlcAsh = 16.8, Ash = 2.44, Color = 5.4,
Flav = 2.98, Hue = 1.07, MalicAcid = 1.77, Mg = 104,
NonFlavPhenols = 0.29, OD = 3.0, Phenols = 2.8,
Proa = 1.87, Proline = 1095)
nw <- predict(pca, newdata = new_wine)[, 1:2]
nw
aggregate(cbind(PC1, PC2) ~ Cultivar, data = scores, FUN = mean)Part 4: PCs as Predictors
Q7. alias() shows bio7 = bio5 − bio6 exactly (annual temperature range = max temperature of the warmest month minus min temperature of the coldest month). lm() cannot separate a variable from an exact combination of two others, returns NA for bio7, and vif() stops with “there are aliased coefficients in the model”. Without bio7, the largest VIF is 7343 (bio11), followed by bio10 (5276) and bio1 (4662): the temperature variables are almost copies of each other.
m_all <- lm(Dry_matter ~ ., data = cassava)
alias(m_all)$Complete
m18 <- lm(Dry_matter ~ . - bio7, data = cassava)
sort(car::vif(m18), decreasing = TRUE)[1:3]Q8. PC1 explains 46% of the climate variation, PC2 29%, PC3 11%.
| Estimate | p | |
|---|---|---|
| PC1 | −0.650 | 9 × 10⁻¹⁹ |
| PC2 | 0.026 | 0.78 |
| PC3 | 0.405 | 0.006 |
All three VIFs are exactly 1 (PC scores are uncorrelated by construction). R² = 0.082. PC1 loads negatively and almost equally on the temperature variables (bio1, bio5, bio6, bio8 to bio11, all about −0.32 to −0.33), so a high PC1 score means a cooler home site. Each unit of PC1 goes with 0.65 less dry matter: landraces from cooler sites have lower root dry matter, those from warmer sites higher. (If your PC1 is flipped, both signs flip and the conclusion is the same.)
pc <- prcomp(cassava[, 1:19], scale. = TRUE)
summary(pc)$importance[2, 1:3]
d <- data.frame(Dry_matter = cassava$Dry_matter, pc$x[, 1:3])
m_pc <- lm(Dry_matter ~ PC1 + PC2 + PC3, data = d)
summary(m_pc)
car::vif(m_pc)
round(sort(pc$rotation[, 1]), 2)
ggplot(d, aes(PC1, Dry_matter)) +
geom_point(alpha = 0.3) +
geom_smooth(method = "lm") +
theme_classic()Q9. The 18-variable model has R² 0.139 against 0.082, but its coefficients are uninterpretable: with VIFs in the thousands, each temperature slope is estimated from tiny differences between near-copies, so the estimates are unstable and their signs mean little. The three-PC model has independent, stable predictors and a clear story (warmer origin, more dry matter), and spends 3 parameters instead of 18. What you give up: a PC is a blend, not a variable you can measure or manipulate, so you cannot say which temperature variable matters; and you keep only the variation the PCs capture, which was chosen to describe the climate, not to predict dry matter (some predictive signal can sit in the discarded PCs).
Part 5: Choosing a Method
Q10. (a) PCA: continuous traits, you want an interpretable axis (the loadings say which traits build it) and scores to reuse as a predictor. Scale first, since the units differ. (b) NMDS (on Bray-Curtis distances): community counts with many zeros, where Euclidean distance and linear axes misbehave; report the stress. PCoA on Bray-Curtis is also defensible. (c) UMAP or t-SNE: tens of thousands of dimensions and a picture is the goal. Use the picture, not the axis values or between-cluster distances. (d) PCoA: when all you have is a distance matrix, PCoA (or NMDS) is the method that starts from distances; PCA needs the raw variables.